package com.kaka.bus.engine.invite;

import com.kaka.bus.bean.CommissionBean;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.InviteBean;
import com.kaka.bus.bean.InviteRule;
import com.kaka.bus.bean.ShareBean;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by OnionMac on 2018/4/16.
 */

public class InvitePresenter extends RxPresenter<InviteContract.View> implements InviteContract.Presenter {

    @Inject
    public InvitePresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void getInviteRule() {
        mCompositeDisposable.add(mDataManager.getApi()
                .getInviteRule()
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<InviteRule>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<InviteRule> wrapper) {
                        mView.onInviteRuleResult(wrapper);
                    }
                }));
    }

    @Override
    public void getInviteCommission(String userId) {
        mCompositeDisposable.add(mDataManager.getApi()
                .getInviteCommission(userId)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<CommissionBean>>() {

                    @Override
                    protected void onSuccess(HttpWrapper<CommissionBean> wrapper) {
                        mView.onInviteCommissionResult(wrapper);
                    }
                }));
    }

    @Override
    public void getInviteShare(String userId) {
        mCompositeDisposable.add(mDataManager.getApi()
                .inviteShare(userId)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<ShareBean>>() {

                    @Override
                    protected void onSuccess(HttpWrapper<ShareBean> wrapper) {
                        mView.onShareResult(wrapper);
                    }

                }));
    }

    @Override
    public void commissionWithdraw(String userId) {
        mCompositeDisposable.add(mDataManager.getApi()
                .inviteCommissionWithdraw(userId)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {

                    @Override
                    protected void onSuccess(HttpWrapper<String> wrapper) {
                        mView.onCommissionWithdrawResult(wrapper);
                    }

                }));
    }

    @Override
    public void getInviteList(String userId, int nowPage, int pageSize, int level) {
        request(userId,nowPage,pageSize,level,false);
    }

    @Override
    public void getInviteListLoadMore(String userId, int nowPage, int pageSize, int level) {
        request(userId,nowPage,pageSize,level,true);
    }


    public void request(String userId, int nowPage, int pageSize, int level, boolean isLoadMore){

        mCompositeDisposable.add(mDataManager.getApi()
                .getInviteList(userId,nowPage,pageSize,level)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<List<InviteBean>>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<List<InviteBean>> data) {
                        if(!isLoadMore){
                            mView.onInviteListResult(data,level);
                        }else{
                            mView.onInviteListLoadMoreResult(data,level);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);
                        if(!isLoadMore){
                            mView.onListError();
                        }else{
                            mView.onListLoadMoreError();
                        }
                    }
                }));
    }


}
